<?php
/*--------------------------------------------------------------------
 度咪科技 - 让工作更轻松快乐
 --------------------------------------------------------------*/

namespace Home\Controller;

class ModuleController extends HomeController
{
    protected $config = array('app_type' => 'master');

    public function index()
    {
        $node = M('Module');
        $menu = array();
        $menu = $node->field('id,pid,name,major,is_del')->where('is_del=0')->order('sort asc, CONVERT(name USING GBK) asc')->select();
        foreach ($menu as &$value) {
            $value['name'] = $value['name'].'-'.$value['major'];
        }
        $tree = list_to_tree($menu);
        $this->assign('menu', popup_tree_menu($tree));

        $model = M('Module');
        $list = $model->where('is_del=0')->order('sort asc')->getField('id,name');
        $this->assign('dept_list', $list);

        $this->display();
    }

    public function del($id)
    {
        $this->_del($id);
    }

    public function winpop()
    {
        $node = M('Module');
        $menu = array();
        $menu = $node->where('is_del=0')->field('id,pid,name')->order('sort asc')->select();

        $tree = list_to_tree($menu);
        $this->assign('menu', popup_tree_menu($tree));
        $pid = array();
        $this->assign('pid', $pid);
        $this->display();
    }

    public function winpop2()
    {
        $this->winpop();
    }

    public function import()
    {
        if (!isset($_FILES['file'])) {
            exit;
        }

        $uploader = new \Home\Lib\Uploader([
            'path' => C('UPLOAD_PATH'),
            'types' => [
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
            ]
        ]);

        $result = $uploader->upload($_FILES['file']);
        if ($result['code'] == 'ok') {
            $result['msg'] = '成功导入数据 '.D('Module')->import($result['src']). ' 条';
        }

        die(json_encode($result));
    }
}
